package zky410;

public class test2 {
    public static void main(String[] args) {
        int[] e = new int[]{1,2,3,4,5};
        int[] c = new int[]{3,4,5,1,2};
        int i = canCompleteRace(e, c);
        System.out.println(i);

    }
    public static int canCompleteRace (int[] e, int[] c) {
        // write code here
        if(e == null || e.length == 0 || c == null || c.length == 0){
            return -1;
        }
        int[] sum = new int[e.length];
        for(int i =0;i<e.length;i++){
            sum[i] = e[i] - c[i];
        }

        for(int i =0;i<e.length;i++){
            if(sum[i]<0){
                continue;
            }
            int temp = i;
            int a = 1;//表示向后移动了几个充电桩
            int res = sum[i];
            while(a < e.length){
                if(temp < e.length-1){
                    temp ++;
                    a++;
                    res += sum[temp];
                    if(res<0){
                        break;
                    }
                }else{
                    temp = 0;
                    a++;
                    res += sum[0];
                    if(res<0){
                        break;
                    }
                }
            }
            if(a == e.length){
                return i;
            }
        }
        return -1;
    }
}

